package com.zygk.library.util;

import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.zygk.library.config.LibraryConstants;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Wikison on 2018/1/19.
 */
public class LibraryHelper {
  static SharedPreferences preferences;
  static SharedPreferences.Editor editor;
  private static String pattern = "yyyy-MM-dd HH:mm:ss";
  public static final Gson gson =
      new GsonBuilder().registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws JsonParseException {
          try {
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            String dateStr = json.getAsString();
            if (!"".equals(dateStr)) {
              return format.parse(dateStr);
            }
          } catch (ParseException e) {
            e.printStackTrace();
          }
          return null;
        }
      }).setDateFormat(pattern).registerTypeAdapter(Object.class, new JsonDeserializer<Double>() {
        public Double deserialize(JsonElement json, Type typeOfT,
            JsonDeserializationContext context) throws JsonParseException {
          try {
            return json.getAsDouble();
          } catch (Exception e) {
            e.printStackTrace();
          }
          return 0d;
        }
      }).create();

  public LibraryHelper(Context context) {
    preferences =
        context.getSharedPreferences(LibraryConstants.APP_SHARED_REFERENCE, Context.MODE_PRIVATE);
    editor = preferences.edit();
  }

  public static final int getSettingInt(String key) {
    return preferences.getInt(key, 0);
  }

  public static final int getSettingInt(String key, int defaultValue) {
    return preferences.getInt(key, defaultValue);
  }

  public static final void setSettingInt(String key, int value) {
    editor.putInt(key, value).commit();
  }

  public static final boolean getSettingBoolean(String key) {
    return preferences.getBoolean(key, false);
  }

  public static final boolean getSettingBoolean(String key, boolean defaultValue) {
    return preferences.getBoolean(key, defaultValue);
  }

  public static final void setSettingBoolean(String key, boolean value) {
    editor.putBoolean(key, value).commit();
  }

  public static final void setSettingString(String key, String status) {
    editor.putString(key, status).commit();
  }

  public static final String getSettingString(String key) {
    return preferences.getString(key, "");
  }

  public static final String getSettingString(String key, String defaultStatus) {
    return preferences.getString(key, defaultStatus);
  }

  public static final Long getSettingLong(String key, Long defaultStatus) {
    return preferences.getLong(key, defaultStatus);
  }

  public static final void setSettingLong(String key, Long value) {
    editor.putLong(key, value).commit();
  }

  public static void clearData() {
    preferences.edit().clear().commit();
  }

  public static UserManager userManager() {
    return UserManager.instance();
  }

  /**
   * 个人
   */
  public static final class User {
    public static class Key {
      public static final String USER_ID = "AUTO_USER_ID";            //用户ID
      public static final String USER_NAME = "USER_NAME";             //登录账号
      public static final String USER_PWD = "USER_PWD";               //登录密码
    }
  }
}
